package com.lottery.im.module.contact.presenter;

import android.app.Activity;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.contact.view.ContactView;
import com.lottery.im.module.tim.event.FriendshipEvent;
import com.lottery.im.module.tim.event.MessageEvent;
import com.lottery.im.module.tim.event.RefreshEvent;
import com.lottery.im.module.tim.presenter.FriendshipManagerPresenter;
import com.lottery.im.module.tim.presenter.GroupManagerPresenter;
import com.lottery.im.module.tim.viewfeatures.FriendshipMessageView;
import com.lottery.im.module.tim.viewfeatures.GroupManageMessageView;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.ext.group.TIMGroupPendencyItem;
import com.tencent.imsdk.ext.sns.TIMFriendFutureItem;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class ContactPresenter extends BaseFragmentPresenter<ContactView> implements
    FriendshipMessageView, GroupManageMessageView,
    Observer {

  private FriendshipManagerPresenter mFriendshipManagerPresenter;
  private GroupManagerPresenter mGroupManagerPresenter;

  public ContactPresenter(Activity activity,
      ContactView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void initManagerPresenter() {
    mFriendshipManagerPresenter = new FriendshipManagerPresenter(this);
    mFriendshipManagerPresenter.getFriendshipLastMessage();
    mGroupManagerPresenter = new GroupManagerPresenter(this);
    mGroupManagerPresenter.getGroupManageLastMessage();
    FriendshipEvent.getInstance().addObserver(this);
    // 群验证 消息 是属于 系统消息
    MessageEvent.getInstance().addObserver(this);
    // 刷新监听
    RefreshEvent.getInstance().addObserver(this);
  }

  @Override public void update(Observable observable, Object data) {
    if (observable instanceof FriendshipEvent) {
      FriendshipEvent.NotifyCmd cmd = (FriendshipEvent.NotifyCmd) data;
      switch (cmd.type) {
        case PROFILE_UPDATE:
        case ADD_REQ:
        case READ_MSG:
        case ADD:
          mFriendshipManagerPresenter.getFriendshipLastMessage();
          break;
      }
      if (cmd.type == FriendshipEvent.NotifyType.ADD
          || cmd.type == FriendshipEvent.NotifyType.PROFILE_UPDATE
          || cmd.type == FriendshipEvent.NotifyType.DEL) {
        if (null == iView) return;
        iView.refreshFriendList();
      }
    } else if (observable instanceof MessageEvent) {
      if (data instanceof TIMMessage) {
        TIMMessage msg = (TIMMessage) data;
        if (msg == null) {
          return;
        }
        if (msg.getConversation().getType() == TIMConversationType.System) {
          mGroupManagerPresenter.getGroupManageLastMessage();
          return;
        }
      }
    } else if (observable instanceof RefreshEvent) {
      if (null == iView) return;
      iView.refreshFriendList();
    }

    //else if (observable instanceof GroupEvent) {
    //  GroupEvent.NotifyCmd cmd = (GroupEvent.NotifyCmd) data;
    //  switch (cmd.type) {
    //    case ADD:
    //    case UPDATE:
    //      mGroupManagerPresenter.getGroupManageLastMessage();
    //      break;
    //  }
    //}
  }

  @Override public void onGetFriendshipLastMessage(TIMFriendFutureItem message, long unreadCount) {
    if (null == iView) return;
    iView.setFriendshipUnread((int) unreadCount);
  }

  @Override public void onGetFriendshipMessage(List<TIMFriendFutureItem> message) {
    mFriendshipManagerPresenter.getFriendshipLastMessage();
  }

  @Override
  public void onGetGroupManageLastMessage(TIMGroupPendencyItem message, long unreadCount) {
    if (null == iView) return;
    iView.setGroupUnread((int) unreadCount);
  }

  @Override public void onGetGroupManageMessage(List<TIMGroupPendencyItem> message) {
    mGroupManagerPresenter.getGroupManageLastMessage();
  }

  public void stop() {
    FriendshipEvent.getInstance().deleteObserver(this);
    MessageEvent.getInstance().deleteObserver(this);
    RefreshEvent.getInstance().deleteObserver(this);
  }
}
